import store from "../store/store";

_data.isPushFlag = {
  20: true,
  30: true,
  40: true,
  50: true,
  70: true,
};

//基础设施获取
_methods.getInfoSubject = (data) => {
  return new Promise((resolve, reject) => {
    _ajax.noLoading(_ajaxUrls.infoSubject.url,
      data,
      (ajData) => {
        let aj = ajData.data;
        if (aj != 0) {
          resolve(aj);
        } else {
          reject(aj);
        }
      },
      _ajaxUrls.infoSubject.type);
  });
};

//推送关闭窗口
_methods.isPushCloseWindow = () => {
  return new Promise((resolve, reject) => {
    let isPush = _methods.getUrlParam("isPush") || false;
    let close = _data.isPushFlag[store.state.showFlag] || false;
    if (isPush == 1 && close) {
      _vrvInit.closeWindow();
      reject();
    } else {
      resolve();
    }
  });
};

//获取反馈统计列表
_methods.feedBackList = (id, downsSet,dt) => {
  let ajUrl = _ajaxUrls.eventFeedbackTable;
  let feedbackType = {
    20: 3,
    30: 2,
    100: 2,
  }[dt.urlType];
  let data = {
    size: 9999,
    page: 1,
    feedbackType,
    downsSet: downsSet,
    articleID: id,
  };
  return new Promise(resolve => {
    _ajax.noLoading(ajUrl.url,
      data,
      (ajData) => {
        // 状态(0--待办；1--已办/已反馈；2--办结)
        let feedBackStatus = {
          0: "待反馈",
          1: "待处理",
          2: "已完成",
        };

        let aj = ajData.result.records;
        let da = aj.map((i) => {
          i.btn = false;
          let s = parseInt(i.status);
          if (i.readTime == null && s != 2) {
            i.stateTxt = "未查看";
          } else {
            i.stateTxt = feedBackStatus[s];
            if (s != 0) {
              i.btn = true;
            }
          }
          return i;
        });
        resolve(da);
      },
      ajUrl.type);
  });
};

_methods.feedBackListDownsSet = (data) => {
  _methods.feedBackList(data.articleID, data.downsSet,data).then(v => {
    store.commit("_downsSet", v);
  });
};

_methods.feedBackListManufacturers = (data) => {
  _methods.feedBackList(data.articleID, data.manufacturers,data).then(v => {
    store.commit("_manufacturers", v);
  });
};

_methods.listOperators = () => {
  let ajUrl = _ajaxUrls.eventFeedbackTable;
  let data = {
    orgID: _data.userInfo.deptId,
  };
  return new Promise(resolve => {
    _ajax.noLoading(ajUrl.url,
      data,
      (ajData) => {
        let aj = ajData.result.records;
        resolve(aj);
      },
      ajUrl.type);
  });
};
_methods.getZjList = () => {
  _ajax.noLoading(_ajaxUrls.zjList.url,
    {},
    (ajData) => {
      store.commit("_zjChecked", {
        txt: "点击选择研判专家",
        val: []
      });
      _data.zjList = ajData.data;
    },
    _ajaxUrls.zjList.type);
};

_methods.getPeoples = (roleID, call) => {
  _ajax.noLoading(_ajaxUrls.userIdGet.url,
    {roleID: roleID},
    (ajData) => {
      call(ajData.result);
    },
    _ajaxUrls.userIdGet.type);
};

//流程详情权限控制
_methods.processSubmitControl = (data) => {
  /**
   * 1  领导                         1  ROLE_LEAD
   * 2  值班员                       2  ROLE_DUTY
   * 3  地方网信办                   3  ROLE_LIAISON
   * 4  设施运营单位                 4  ROLE_UNIT
   * 5  安全厂商                     5  ROLE_SAFETY
   * 6  专家                         6  ROLE_EXPERT
   *
   * 1：值班员审批（等待审批）
   * 2：领导审批（等待审批）
   * 3：审批完成
   * 4：等待反馈（下发中）
   * 5：反馈完毕（下发完成）
   * 6：已归档（办结）
   * */
  let control = {
    leaderAdvise: false,
    leaderSubmit: false,
    zbyAsk: false,
    zbyRelease: false,
    zbyComplete: false,
    replyClaim: false,
    handleClaim: false,
    zjJudge: false,
    zjJudgeList: false,
    dwSubmit: false,
    dwPush: false,
    temSubmit: false,
    endShow: false,
  };
  let role = window._data.userInfo.roleRank;
  let st = parseInt(data.status);
  if (role === 1 || role === 2) {
    control.leaderAdvise = true;
  }

  if (data.approvalUserID == window._data.userInfo.userId) {
    //领导
    if (role === 1) {
      if (st !== 3) {
        control.leaderSubmit = true;
      }
      if (st == 6) {
        control.endShow = true;
      }
      control.zjJudgeList = true;
      control.replyClaim = true;
      control.handleClaim = true;
    }

    //值班员
    if (role === 2) {
      control.zjJudgeList = true;
      switch (st) {
        case 1:
          control.zbyAsk = true;
          break;
        case 3:
          store.commit("_feedBackItemCheck", []);
          store.commit("_feedBackItemCheckSafe", []);
          store.commit("_orgChecked", {
            txt: "点击选择响应单位",
            val: []
          });
          store.commit("_dwChecked", {
            txt: `点击选择安全厂商`,
            val: []
          });
          _data.orgChecked = [];
          _data.csChecked = [];
          control.zbyRelease = true;
          break;
        case 5:
          control.zbyComplete = true;
          break;
        case 6:
          control.endShow = true;
          break;
      }
      if (st > 3) {
        control.replyClaim = true;
        control.handleClaim = true;
        store.commit("_downsSet", false);
        store.commit("_manufacturers", false);
        if (data.downsSet) {
          _methods.feedBackListDownsSet(data);
        }
        if (data.manufacturers) {
          _methods.feedBackListManufacturers(data);
        }
      }
    }

    //专家
    if (role === 6) {
      control.zjJudgeList = true;
      if (data.isJudged == '0') {
        control.zjJudge = true;
      }
    }
  }

  //运营单位
  if (role === 3 || role === 4 || role === 5) {
    control.temSubmit = true;
    if (st == 0) {
      if (role === 3) {
        store.commit("_orgChecked", {
          txt: "点击选择响应单位",
          val: []
        });
        _data.orgChecked = [];
        control.dwPush = true;
      }
      control.dwSubmit = true;
    }
  }

  return control;
};


